﻿using System;
using Wpf.Ui;
using Wpf.Ui.Controls;

namespace NxChatQQ.Services
{
    public class SimpleSnackbarService : ISnackbarService
    {
        private SnackbarPresenter _presenter;

        private Snackbar _snackbar;

        public TimeSpan DefaultTimeOut { get; set; } = TimeSpan.FromSeconds(3);

        /// <inheritdoc />
        public void SetSnackbarPresenter(SnackbarPresenter contentPresenter)
        {
            _presenter = contentPresenter;
            //contentPresenter.FindResource();
        }

        /// <inheritdoc />
        public SnackbarPresenter GetSnackbarPresenter()
        {
            return _presenter;
        }

        public void ShowSuccess(string message)
        {
            Show(null, message, ControlAppearance.Success, new SymbolIcon(SymbolRegular.CheckmarkCircle20, filled: true), DefaultTimeOut);
        }

        public void ShowError(string message)
        {
            Show(null, message, ControlAppearance.Danger, new SymbolIcon(SymbolRegular.DismissCircle20, filled: true), DefaultTimeOut);
        }
        public void ShowWarn(string message)
        {
            Show(null, message, ControlAppearance.Caution, new SymbolIcon(SymbolRegular.Warning20, filled: true), DefaultTimeOut);
        }

        public void ShowInfo(string message)
        {
            Show(null, message, ControlAppearance.Info, new SymbolIcon(SymbolRegular.Info20, filled: true), DefaultTimeOut);
        }


        public void Show(string title, string message, ControlAppearance appearance, IconElement icon, TimeSpan timeout)
        {
            if (_presenter is null)
            {
                throw new InvalidOperationException($"The SnackbarPresenter was never set");
            }

            if (_snackbar == null)
            {
                _snackbar = new Snackbar(_presenter);
            }
            _snackbar.SetCurrentValue(Snackbar.TitleProperty, title);
            _snackbar.SetCurrentValue(Snackbar.TitleProperty, title);
            _snackbar.SetCurrentValue(System.Windows.Controls.ContentControl.ContentProperty, message);
            _snackbar.SetCurrentValue(Snackbar.AppearanceProperty, appearance);
            _snackbar.SetCurrentValue(Snackbar.IconProperty, icon);
            _snackbar.SetCurrentValue(
                Snackbar.TimeoutProperty,
                timeout.TotalSeconds == 0 ? DefaultTimeOut : timeout
            );

            _snackbar.Show(true);
        }
    }
}
